/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.CenterDao;
import cc.altius.model.HealthSpringCenter;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author sagar
 */
@Repository("CenterDao")
public class CenterDaoImpl implements CenterDao {

    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    @Autowired
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    private Session session;
    private Query query;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional
    public int addCenter(HealthSpringCenter center) {
        return (Integer) this.hibernateTemplate.save(center);
    }

    @Override
    @Transactional
    public HealthSpringCenter getCenterById(int centerId) {
        session = this.sessionFactory.getCurrentSession();
        query = session.createQuery("from HealthSpringCenter where centerId=" + centerId);
        return (HealthSpringCenter) query.uniqueResult();
    }

    @Override
    @Transactional
    public List<HealthSpringCenter> getCenterList(int cityId) {
        List<HealthSpringCenter> center;
        if (cityId == 0) {
            center = this.hibernateTemplate.find("from HealthSpringCenter");
        } else {
            center = this.hibernateTemplate.find("from HealthSpringCenter where masterZone.cityId.cityId=" + cityId);
        }
        return center;
    }

    @Override
    @Transactional
    public int updateCenter(HealthSpringCenter center, int centerId) {
        session = this.sessionFactory.getCurrentSession();
        String hql = "Update "
                + " HealthSpringCenter as hsc "
                + " set "
                + " hsc.centerName=:centerName,"
                + " hsc.latitude=:latitude,"
                + " hsc.longitude=:longitude,"
                + " hsc.address=:address, "
                + " hsc.phoneNumber=:phoneNumber, "
                + " masterZone.masterZoneId=:masterZoneId "
                + " WHERE hsc.centerId=:centerId";
        query = session.createQuery(hql);
        query.setParameter("centerName", center.getCenterName());
        query.setParameter("latitude", center.getLatitude());
        query.setParameter("longitude", center.getLongitude());
        query.setParameter("address", center.getAddress());
        query.setParameter("phoneNumber", center.getPhoneNumber());
        query.setParameter("masterZoneId", center.getMasterZone().getMasterZoneId());
        query.setParameter("centerId", center.getCenterId());
        int rowCount = query.executeUpdate();
        System.out.println("rowcount" + rowCount);
        return rowCount;
    }

    @Override
    @Transactional
    public List<HealthSpringCenter> getCenterList() {
        List<HealthSpringCenter> center = this.hibernateTemplate.find("from HealthSpringCenter");
        return center;
    }

    @Override
    @Transactional
    public String getEmailIdOfCentre(int centerId) {
        session = this.sessionFactory.getCurrentSession();
        query = session.createQuery("select center.emailId from HealthSpringCenter center where center.centerId=" + centerId);
        return (String) query.uniqueResult();
    }
}
